﻿//=====================================================================================================
// LocalizationLibrary
// The Localization Library is a collection of reusable software components that provide 
// support for localization. This library enables you to localize WPF, Silverlight and WP7 applications.
// (http://localizationlibrary.codeplex.com)
//=====================================================================================================
// Author: Nicola Gallo (Website: www.nicola.gallo.name, Email: nicolagmt@hotmail.it) 
//=====================================================================================================

namespace ReflectionStudio.LocalizationLibrary
{
    //.NET References
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    //LocalizationLibrary References
    using ReflectionStudio.LocalizationLibrary.Attributes;
    using LocalizationLibrary.Messaging;

    /// <summary>
    /// Defines dynamic resouces' table.
    /// </summary>
    internal class DynamicResourcesTable
    {
        //Fields

        private Dictionary<String, DynamicResource> _registeredDynamicResources;

        //Properties

        /// <summary>
        /// Gets registered dynamic resources' dictionary.
        /// </summary>
        protected Dictionary<String, DynamicResource> RegisteredDynamicResources
        {
            get
            {
                if (this._registeredDynamicResources == null)
                {
                    this._registeredDynamicResources = new Dictionary<string, DynamicResource>();
                }
                return this._registeredDynamicResources;
            }
        }

        //Indexers

        /// <summary>
        /// Gets the <paramref name="DynamicResource"/>
        /// </summary>
        /// <param name="resourceID">Resource identifier.</param>
        /// <returns>Return the instance if exist, Null otherwise.</returns>
        public DynamicResource this[String resourceID]
        {
            get
            {
                DynamicResource dynamicResource = null;
                if (this.RegisteredDynamicResources.ContainsKey(resourceID))
                {
                    dynamicResource = this.RegisteredDynamicResources[resourceID];
                }
                return dynamicResource;
            }
        }

        //Methods

        /// <summary>
        /// Adds the input <paramref name="statiResource"/> into <see cref="DynamicResourceTable.RegisteredStaticResources"/>.
        /// </summary>
        /// <param name="dynamicResource">Dynamic resource.</param>
        /// <returns>True whether adds the resource, False otherwise.</returns>
        /// <param name="publishNotify">If true the <see cref="MessagingManager"/> publish the notify.</param>
        public bool Add(DynamicResource dynamicResource, bool publishNotify)
        {
            bool retVal = false;
            if ((dynamicResource != null) && (!this.RegisteredDynamicResources.ContainsKey(dynamicResource.ResourceID)))
            {
                this.RegisteredDynamicResources.Add(dynamicResource.ResourceID, dynamicResource);
                if (publishNotify)
                {
                    MessagingManager.Publish(new ResourceMessage(dynamicResource, Operation.Created));    
                }                
                retVal = true;
            }
            return retVal;
        }

        /// <summary>
        /// Update the dyanamic resource's table with the input <paramref name="token"/>.
        /// </summary>
        /// <param name="token">Defines the token for updating.</param>
        /// <param name="publishNotify">If true the <see cref="MessagingManager"/> publish the notify.</param>
        public void Update(DynamicResourceToken token, bool publishNotify)
        {
            if ((token != null) && (this.RegisteredDynamicResources.ContainsKey(token.ResourceID)))
            {
                DynamicResource dynamicResource = this.RegisteredDynamicResources[token.ResourceID];
                dynamicResource.Add(token.Language, token.Value, VersionConflictPolicy.KeepExisting, publishNotify);
            }
        }

        /// <summary>
        /// Gets all the <see cref="DynamicResource"/> unlocalized for the input language.
        /// </summary>
        /// <param name="language">Specifies the language.</param>
        /// <returns>All the <see cref="DynamicResource"/> unlocalized for the input language.</returns>
        public DynamicResource[] GetAllUnlocalizedResource(Language language) 
        {
            List<DynamicResource> unlocalizedResourecs = new List<DynamicResource>();
            if (language != null)
            {
                foreach (var item in this.RegisteredDynamicResources)
                {
                    Object value = item.Value[language];
                    if (value == null)
                    {
                        unlocalizedResourecs.Add(item.Value);
                    }
                }    
            }
            return unlocalizedResourecs.ToArray();
        }

        /// <summary>
        /// Resets the <see cref="DynamicResourcesTable"/>.
        /// </summary>
        public void Reset()
        {
            lock (this.RegisteredDynamicResources)
            {
                this.RegisteredDynamicResources.Clear();
            }
        }
    }
}